// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Eggman Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 71 : TYPE_EGGMAN

// Function declarations
reserve function Eggman_Function163
reserve function Eggman_Function164

// Static Values

// Tables
public table Eggman_table36
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0
end table


function Eggman_Function163
	Sin256(temp0, object.value3)
	temp0 <<= 10
	object.ypos += temp0
	object.value3 += 2
	object.value3 &= 255
end function


function Eggman_Function164
	object.value0 += object.xvel
	object.value1 += object.yvel
	object.xpos = object.value0
	object.ypos = object.value1
end function


event ObjectMain
	switch object.state
	case 0
		GetTableValue(temp0, 0, Eggman_table36)
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object[currentPlayer].xpos >= temp0
				object.xvel = 0x6000
				object.yvel = -0x18000
				object.value1 = object.ypos
				object.value3 = 0
				object.state++
				object.priority = PRIORITY_ACTIVE
				ResetObjectEntity(26, TypeName[Music Event], 0, 0, 0)
				object[26].priority = PRIORITY_ACTIVE
				stage.newWaterLevel = 0x1280000
			end if
		next
		CallFunction(Eggman_Function164)
		if object.state == 0
			CallFunction(Eggman_Function163)
		end if
		break
	case 1
		temp1 = -2
		GetTableValue(temp0, 1, Eggman_table36)
		if object.value0 >= temp0
			object.value0 = temp0
			object.xvel = 0
			temp1++
		end if
		GetTableValue(temp0, 2, Eggman_table36)
		if object.value1 <= temp0
			object.value1 = temp0
			object.yvel = 0
			temp1++
		end if
		if temp1 == 0
			object.xvel = 0x14000
			object.yvel = -0x20000
			object.state++
		end if
		CallFunction(Eggman_Function164)
		break
	case 2
		temp1 = -2
		GetTableValue(temp0, 3, Eggman_table36)
		if object.value0 >= temp0
			object.value0 = temp0
			object.xvel = 0
			temp1++
		end if
		GetTableValue(temp0, 4, Eggman_table36)
		if object.value1 <= temp0
			object.value1 = temp0
			object.yvel = 0
			temp1++
		end if
		if temp1 == 0
			object.yvel = -0x18000
			object.state++
		end if
		CallFunction(Eggman_Function164)
		break
	case 3
		GetTableValue(temp0, 5, Eggman_table36)
		if object.value1 <= temp0
			object.value1 = temp0
			if object.value2 == 0
				object.xvel = 0x14000
				object.yvel--
			else
				object.xvel = 0x28000
				object.yvel--
			end if
			object.direction = FLIP_X
			object.value0 = object.xpos
			object.state++
			CallFunction(Eggman_Function164)
		else
			object.value4 += 2
			object.value4 &= 255
			Cos256(temp0, object.value4)
			if temp0 > 0
				object.direction = FLIP_X
			else
				object.direction = FLIP_NONE
			end if
			Sin256(object.xpos, object.value4)
			object.xpos <<= 12
			object.xpos += object.value0
			temp0 = object.yvel
			temp1 = 0x7FFFFFFF
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				if object[currentPlayer].ypos < temp1
					temp1 = object[currentPlayer].ypos
				end if
			next
			temp1 -= object.value1
			if temp1 >= 0
				temp1 -= 0x480000
				if temp1 >= 0
					temp0 >>= 1
					temp1 -= 0x280000
					if temp1 >= 0
						temp0 >>= 1
						temp1 -= 0x280000
						if temp1 >= 0
							temp0 = 0
						end if
					end if
				end if
			end if
			if object.value2 == 1
				temp0 <<= 1
			end if
			if temp0 == 0
				object.ypos = object.value1
				CallFunction(Eggman_Function163)
			else
				object.value1 = object.ypos
				object.value3 = 0
				object.value1 += temp0
				object.ypos = object.value1
			end if
		end if
		break
	case 4
		temp1 = -2
		GetTableValue(temp0, 6, Eggman_table36)
		if object.value0 >= temp0
			object.value0 = temp0
			object.xvel = 0
			temp1++
		end if
		GetTableValue(temp0, 7, Eggman_table36)
		if object.value1 <= temp0
			object.value1 = temp0
			object.yvel = 0
			temp1++
		end if
		if temp1 == 0
			if object.value2 == 0
				object.direction = FLIP_NONE
			end if
			object.value3 = 0
			object.state++
		end if
		CallFunction(Eggman_Function164)
		break
	case 5
		temp2 = 0
		if object.value2 == 0
			GetTableValue(temp0, 8, Eggman_table36)
			GetTableValue(temp1, 9, Eggman_table36)
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				if object[currentPlayer].xpos >= temp0
					if object[currentPlayer].ypos <= temp1
						temp2 = 1
						object.value5 = 50
						object.animation = 4
						object.animationTimer = 32
					end if
				end if
			next
		else
			temp2 = 1
			object.value5 = 1
		end if
		if temp2 == 1
			ResetObjectEntity(26, TypeName[Music Event], 1, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			object.state++
		end if
		CallFunction(Eggman_Function164)
		CallFunction(Eggman_Function163)
		break
	case 6
		object.value5--
		if object.value5 == 0
			object.value5 = 0
			object.xvel = 0x40000
			object.yvel = -0x4000
			object.priority = PRIORITY_ACTIVE
			object.animation = 6
			object.animationTimer = 0
			object.value6 = 2
			object.value1 = object.ypos
			object.state++
			if stage.debugMode == 0
				if object.value10 == 0
					CallNativeFunction2(SetAchievement, 6, 100)
				end if
			end if
		else
			if object.value5 == 24
				object.animation = 1
				object.animationTimer = 0
				object.direction = FLIP_X
			end if
		end if
		CallFunction(Eggman_Function164)
		if object.state == 6
			CallFunction(Eggman_Function163)
		end if
		break
	case 7
		if object.outOfBounds == 1
			ResetObjectEntity(object.entityPos, TypeName[Blank Object], 0, 0, 0)
			object.value2 = 0
		else
			CallFunction(Eggman_Function164)
		end if
		break
	end switch
	if object.value8 != 0
		if object.value9 > 0
			object.value9--
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.value9 == 0
				CheckEqual(object[currentPlayer].animation, ANI_HURT)
				temp0 = checkResult
				CheckEqual(object[currentPlayer].animation, ANI_DYING)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].animation, ANI_DROWNING)
				temp0 |= checkResult
				if temp0 != 0
					if object.animation != 2
						object.animation = 2
						object.animationTimer = 0
					end if
					if currentPlayer == 0
						object.value10 = 1
					end if
				end if
				BoxCollisionTest(C_TOUCH, object.entityPos, -20, -16, 20, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 1
					CallFunction(PlayerObject_CheckHit)
					if checkResult == 1
						object.value8--
						if object.value8 == 0
							player.score += 1000
							object.animation = 4
							object.animationTimer = 0
							object.value2 = 1
						else
							object.animation = 3
							object.animationTimer = 0
							object.value9 = 32
							PlaySfx(SfxName[Boss Hit], 0)
						end if
					end if
				end if
			end if
		next
	end if
	if object.value2 == 1
		temp0 = oscillation
		temp0 &= 7
		if temp0 == 0
			Rand(temp0, 96)
			temp0 -= 48
			temp0 <<= 16
			temp0 += object.xpos
			Rand(temp1, 48)
			temp1 -= 24
			temp1 <<= 16
			temp1 += object.ypos
			CreateTempObject(TypeName[Explosion], 0, temp0, temp1)
			PlaySfx(SfxName[Explosion], 0)
		end if
	end if
end event


event ObjectDraw
	switch object.animation
	case 1
		temp0 = object.animationTimer
		if saveRAM[495] == 0
			temp0 /= 6
		else
			temp0 /= 24
		endif
		object.animationTimer++
		if saveRAM[495] == 0
			object.animationTimer %= 12
		else
			object.animationTimer %= 48
		endif
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		temp0 = object.animationTimer
		if saveRAM[495] == 0
			temp0 %= 10
			temp0 /= 5
		else
			temp0 %= 20
			temp0 /= 10
		endif
		temp0 += 9
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			if object[0].animation != ANI_DYING
				object.animation = 1
			end if
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 3
		temp0 = object.animationTimer
		temp0 &= 1
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 30
			object.animationTimer = 0
			object.animation = 1
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 4
		temp0 = object.animationTimer
		temp0 >>= 5
		temp0 += 4
		object.animationTimer++
		object.animationTimer &= 63
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 5
		DrawSpriteFX(6, FX_FLIP, object.xpos, object.ypos)
		break
	case 6
		temp0 = object.animationTimer
		temp0 >>= 2
		temp0 += 7
		object.animationTimer++
		object.animationTimer %= 7
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	end switch
	switch object.value6
	case 1
		temp0 = object.value7
		temp0 >>= 2
		temp0 += 11
		object.value7++
		object.value7 &= 7
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		temp0 = object.value7
		temp0 /= 3
		temp0 += 11
		object.value7++
		if object.value7 > 21
			object.value7 = 18
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	end switch
end event


event ObjectStartup
	if saveRAM[495] == 0
		LoadSpriteSheet("Global/Eggman.gif")
	else
		LoadSpriteSheet("Global/EggRobo.gif")
	endif
	SpriteFrame(-28, -32, 64, 56, 1, 1)
	SpriteFrame(-28, -32, 64, 56, 66, 1)
	SpriteFrame(-28, -32, 64, 56, 131, 1)
	SpriteFrame(-28, -32, 64, 56, 1, 58)
	SpriteFrame(-28, -32, 64, 56, 1, 1)
	SpriteFrame(-28, -32, 64, 56, 1, 58)
	SpriteFrame(-28, -32, 64, 56, 66, 58)
	SpriteFrame(-28, -32, 64, 56, 131, 58)
	SpriteFrame(-28, -32, 64, 56, 66, 1)
	SpriteFrame(-28, -32, 64, 56, 1, 115)
	SpriteFrame(-28, -32, 64, 56, 66, 115)
	SpriteFrame(34, 1, 10, 9, 196, 2)
	SpriteFrame(33, 0, 15, 11, 196, 12)
	SpriteFrame(34, -1, 22, 14, 213, 1)
	SpriteFrame(34, -8, 32, 26, 213, 16)
	SpriteFrame(34, -1, 22, 14, 213, 1)
	SpriteFrame(34, -8, 32, 26, 213, 16)
	SpriteFrame(34, 1, 10, 9, 196, 2)
	SpriteFrame(33, 0, 15, 11, 196, 12)
	foreach (TypeName[Eggman], arrayPos0, ALL_ENTITIES)
		temp0 = -0x700000
		temp0 += object[arrayPos0].xpos
		SetTableValue(temp0, 0, Eggman_table36)
		temp0 = 0x380000
		temp0 += object[arrayPos0].xpos
		SetTableValue(temp0, 1, Eggman_table36)
		temp0 = -0xC00000
		temp0 += object[arrayPos0].ypos
		SetTableValue(temp0, 2, Eggman_table36)
		temp0 = 0x600000
		temp0 += object[arrayPos0].xpos
		SetTableValue(temp0, 3, Eggman_table36)
		temp0 = -0x1000000
		temp0 += object[arrayPos0].ypos
		SetTableValue(temp0, 4, Eggman_table36)
		temp0 = -0x4C00000
		temp0 += object[arrayPos0].ypos
		SetTableValue(temp0, 5, Eggman_table36)
		temp0 = 0x13C0000
		temp0 += object[arrayPos0].xpos
		SetTableValue(temp0, 6, Eggman_table36)
		temp0 = -0x5000000
		temp0 += object[arrayPos0].ypos
		SetTableValue(temp0, 7, Eggman_table36)
		temp0 = 0xB80000
		temp0 += object[arrayPos0].xpos
		SetTableValue(temp0, 8, Eggman_table36)
		temp0 = -0x4D00000
		temp0 += object[arrayPos0].ypos
		SetTableValue(temp0, 9, Eggman_table36)
		object[arrayPos0].value0 = object[arrayPos0].xpos
		object[arrayPos0].value1 = object[arrayPos0].ypos
		object[arrayPos0].animation = 1
		object[arrayPos0].value8 = 8
	next
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
